/***************************************************************************************
**  Criado por: Rodrigo Trudelmer (www.trudelmer.com.br)                              **
**  baseado em outros fontes retirados da internet                                    **
**  obrigado ao seus respectivos autores/criadores                                    **
**      http://lucasoliver.wordpress.com/2008/11/03/applet-para-impressao-via-web/    **
**      http://evandropaes.wordpress.com/2009/06/24/criando-arquivos-jar/             **
**      http://javafree.uol.com.br/topic-875000-Impressao-via-Applet.html             **
**      http://www.guj.com.br/posts/list/57965.java                                   **
**  Pelotas - RS                                                                      **
**  27/04/2010                                                                        **
****************************************************************************************/

import java.awt.Font;  
import java.awt.Graphics;  
import java.awt.Graphics2D;  
import java.awt.print.Book;  
import java.awt.print.PageFormat;  
import java.awt.print.Paper;  
import java.awt.print.Printable;  
import java.awt.print.PrinterException;  
import java.awt.print.PrinterJob;  
import javax.swing.JApplet;  
 
public class phpjpg extends JApplet implements Printable { 

	 private String valueArray = null;
	 private String text = null;
	 private int vlx = 0;
	 private int vly = 0;
	 private String namefont = null;
	 private int sizefont = 8;
	 private int addx = 0;
	 private int addy = 0;
	 private String[] ts = null; 
     
     private Book book;  
     public double fatorConverMMPt = 2.834646D;  
     @Override  
     public void init() {  
       imprimir();  
     }  
     public void imprimir() {  
         PrinterJob impressoraPadrao = PrinterJob.getPrinterJob();  
         impressoraPadrao.setPrintable(this);  
         book = new Book();  
         PageFormat pageFormat = new PageFormat();   
         Paper Folha = new Paper();  
         double width  = Double.parseDouble(getParameter("pagewidth"));  
         double height = Double.parseDouble(getParameter("pageheight"));  
         width  = width * fatorConverMMPt;  
         height = height * fatorConverMMPt;  
         Folha.setSize(width, height);  
         Folha.setImageableArea(0.0, 0.0, width, height);  
         pageFormat.setPaper(Folha);  
         book.append(this, pageFormat);  
         impressoraPadrao.setPageable(book);  
         try {  
             impressoraPadrao.print();  
         } catch (Exception e) {  
             e.printStackTrace();  
         }  
     }  
   
     public int print(Graphics g, PageFormat format, int pageIndex) throws PrinterException {  
         Graphics2D eventoGrafico = (Graphics2D) g;   
         eventoGrafico.translate(format.getImageableX(), format.getImageableX());   

		 addx = Integer.parseInt(getParameter("paddx"));
		 addy = Integer.parseInt(getParameter("paddy"));
		 valueArray = getParameter("valuePrint");
		 
		 ts = valueArray.split("#");
		 for(int i=0; i<ts.length; i=(i+5)){
			vlx = Integer.parseInt(ts[i]);
			vly = Integer.parseInt(ts[i+1]);
			namefont = ts[i+2];
			sizefont = Integer.parseInt(ts[i+3]);
			text = ts[i+4];
			
			vlx = vlx + addx;
			vly = vly + addy;
			
			eventoGrafico.setFont(new Font(namefont, Font.PLAIN, sizefont)); 
			eventoGrafico.drawString(text, vlx, vly);
		 }

         return PAGE_EXISTS;  
     }  
}   